#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2016-2019, Intel Corporation
#
#
# obj_rpmem_basic_integration/TEST6 -- rpmem replication to two remote replicas
#    with initially zeroed pool files
#

. ../unittest/unittest.sh

require_test_type medium

setup

require_nodes 3

require_node_libfabric 0 $RPMEM_PROVIDER
require_node_libfabric 1 $RPMEM_PROVIDER
require_node_libfabric 2 $RPMEM_PROVIDER

init_rpmem_on_node 0 1 2

# binary for this test
EXE=obj_basic_integration

# define files and directories
TEST_SET_LOCAL="testset_local"
TEST_SET_REMOTE="testset_remote"

TEST_FILE_LOCAL="testfile_local"
TEST_FILE_REMOTE="testfile_remote"

# create and upload poolset files
create_poolset $DIR/$TEST_SET_LOCAL 16M:${NODE_DIR[0]}$TEST_FILE_LOCAL:x \
        m ${NODE_ADDR[1]}:${TEST_SET_REMOTE}1 \
	m ${NODE_ADDR[2]}:${TEST_SET_REMOTE}2
create_poolset $DIR/${TEST_SET_REMOTE}1 17M:${NODE_DIR[1]}/${TEST_FILE_REMOTE}1:x
create_poolset $DIR/${TEST_SET_REMOTE}2 17M:${NODE_DIR[2]}/${TEST_FILE_REMOTE}2:x

copy_files_to_node 0 ${NODE_DIR[0]} $DIR/$TEST_SET_LOCAL
copy_files_to_node 1 ${NODE_DIR[1]} $DIR/${TEST_SET_REMOTE}1
copy_files_to_node 2 ${NODE_DIR[2]} $DIR/${TEST_SET_REMOTE}2

rm_files_from_node 0 ${NODE_DIR[0]}$TEST_FILE_LOCAL
rm_files_from_node 1 ${NODE_DIR[1]}${TEST_FILE_REMOTE}1
rm_files_from_node 2 ${NODE_DIR[2]}${TEST_FILE_REMOTE}2

# create remote holey pool files
create_holey_file_on_node 0 16M ${NODE_DIR[0]}$TEST_FILE_LOCAL
create_holey_file_on_node 1 17M ${NODE_DIR[1]}${TEST_FILE_REMOTE}1
create_holey_file_on_node 2 17M ${NODE_DIR[2]}${TEST_FILE_REMOTE}2

# execute test
expect_normal_exit run_on_node 0 ./$EXE$EXESUFFIX ${NODE_DIR[0]}$TEST_SET_LOCAL

check

# download pools and compare them
copy_files_from_node 0 $DIR ${NODE_DIR[0]}$TEST_FILE_LOCAL
copy_files_from_node 1 $DIR ${NODE_DIR[1]}${TEST_FILE_REMOTE}1
copy_files_from_node 2 $DIR ${NODE_DIR[2]}${TEST_FILE_REMOTE}2

compare_replicas "-soOaAb -l -Z -H -C" \
	$DIR/$TEST_FILE_LOCAL $DIR/${TEST_FILE_REMOTE}1 > diff$UNITTEST_NUM.log

compare_replicas "-soOaAb -l -Z -H -C" \
	$DIR/$TEST_FILE_LOCAL $DIR/${TEST_FILE_REMOTE}2 >> diff$UNITTEST_NUM.log

check_local

pass
